﻿using System;
using System.IO;
using System.Text;

namespace mytest2.webs
{
    public class MyUtils
    {
        internal static UserInfoVO obtianUserInfo(string domainAndName)
        {
            string[] infoes = domainAndName.Split(new char[1] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
            string userDomainName = "";
            string userName = "";
            if (infoes.Length > 1)
            {
                userDomainName = infoes[0];
                userName = infoes[1];
            }

            WriteTextLog("OEMS AD登录信息收集：", userDomainName + "|" + userName, DateTime.Now);
            if (userDomainName != "" && userName != "")
            {
                
                long timestamp = (DateTimeOffset.Now.ToUnixTimeSeconds());

                string md5bef = "secure" + timestamp + userName; //加密前数据
                string md5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(md5bef, "MD5"); //加密后数据

                UserInfoVO result = new UserInfoVO();
                result.timestamp = timestamp;
                result.userDomainName = userDomainName;
                result.userName = userName;
                result.md5 = md5;
                return result;

            }
            else
            {
                
                return null;

            }
             
        }
        public static void WriteTextLog(string action, string strMessage, DateTime time)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + @"LogMobileInterfaceLog\";
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            string fileFullPath = path + time.ToString("yyyy-MM-dd") + ".System.txt";
            StringBuilder str = new StringBuilder();
            str.Append("Time:    " + time.ToString() + "\r\n");
            str.Append("Action:  " + action + "\r\n");
            str.Append("Message: " + strMessage + "\r\n");
            str.Append("-----------------------------------------------------------\r\n\r\n");
            StreamWriter sw;
            if (!File.Exists(fileFullPath))
            {
                sw = File.CreateText(fileFullPath);
            }
            else
            {
                sw = File.AppendText(fileFullPath);
            }
            sw.WriteLine(str.ToString());
            sw.Close();
        }
    }
}